﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADO.NET.Samples.Common;

namespace ADO.NET.Samples.Forms.LINQ
{
    [FormDescription("LINQ to Untyped DataSet", "Demonstrate how to use LINQ to query data from untyped DataSet and bind data to DataGridView.")]
    public partial class LINQtoUntypedDataSet : Form
    {
        public LINQtoUntypedDataSet()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Data binding
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LINQtoUntypedDataSet_Load(object sender, EventArgs e)
        {
            var ds = DBCommon.GetArticleAndUserData();
            var articles = from article in ds.Tables["Article"].AsEnumerable()
                           join user in ds.Tables["User"].AsEnumerable()
                           on article.Field<int>("UserID") equals user.Field<int>("UserID")
                           let Author = user.Field<string>("UserName")
                           orderby article.Field<int>("ArticleID") descending
                           select new { Author, Title = article.Field<string>("ArticleTitle") };
            foreach (var item in articles)
            {
                this.listBox1.Items.Add(string.Format("{0}(Author: {1})", item.Title, item.Author));
            }
        }
    }
}
